/* ======================================================= */
/**
 * File   : ToolBox.java
 * Date   : 3 mars 2010 at 10:45:18
 */
/* ======================================================= */
package fr.isima.isipaint.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;

import fr.isima.isipaint.controllers.ToolsController;
import fr.isima.isipaint.tools.ToolBox;

/* ======================================================= */
/**
 * 
 * @author Daniel
 * 
 */
/* ======================================================= */
public class ToolBoxPanel extends JPanel {

	/** Serial number */
	private static final long serialVersionUID = 1L;

	/** The JPanel to display the buttons */
	private JPanel buttonPanel = new JPanel();

	/** The number of columns for the toolBox */
	private static final int NB_COLUMNS = 2;

	/** The width of one button */
	private static final int BUTTON_WIDTH = 30;

	/** The height of one button */
	private static final int BUTTON_HEIGHT = 30;

	/** The tools set */
	private ToolBox tools;

	/** The listener */
	private ToolsController toolsCtrl;

	/**
	 * Default Constructor
	 */
	public ToolBoxPanel(ToolBox toolBox, ToolsController toolsCtrl) {
		this.tools = toolBox;
		this.toolsCtrl = toolsCtrl;

		/* Jpanel for buttons */
		add(buttonPanel);
		buttonPanel.setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();

		int i = 0;
		int j = -1;

		/* To display the buttons */
		for (AbstractAction t : tools.getTools()) {
			JButton button = new JButton();
			button.setAction(t);
			button.setText(""); // To only display the icon
			button.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
			button.addActionListener(toolsCtrl);
			constraints.gridx = i++ % NB_COLUMNS;
			constraints.gridy = (i == 0) ? j++ : j;
			buttonPanel.add(button, constraints);
		}

	}
}
